/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.patterns;


import org.digitalsoul.switchplugin.SwitchFile;


public class FilenameSuffixPattern implements IPattern {

    private String suffix;
    
    public FilenameSuffixPattern(String suffix) {
        this.suffix = suffix;
    }

    public SwitchFile applyToFile(IPattern previousPattern, SwitchFile file) {
        file.changeBaseName(file.getBaseName() + suffix);
        return file;
    }

    public boolean matches(SwitchFile file) {
        return file.getBaseName().matches(".*" + suffix + "$"); 
    }

    public SwitchFile undoFile(SwitchFile file) {
        int index = file.getBaseName().lastIndexOf(suffix);
        file.changeBaseName(file.getBaseName().substring(0, index));
        return file;
    }
    
    @Override
    public String toString() {
        return "filename-suffix: " + suffix;
    }

    public String getSuffix() {
        return suffix;
    }
    
    public PatternType getType() {
        return PatternType.FILENAME_SUFFIX;
    }
}
